/*
 * ToolsLayer.h
 *
 *  Created on: 2010-9-4
 *      Author: try
 */

#ifndef TOOLSLAYER_H_
#define TOOLSLAYER_H_

#include "WorkPage.h"
#include "SimpleButton.h"
#include "../../commons.h"
#include "../../gdiplus/gdraw.h"

using namespace std;
using namespace Ed;
using namespace Thk;

namespace S {
  /**
   * 工具栏
   */
  class ToolsLayer: public Ed::Layer {
    public:
      ToolsLayer(WorkPage* page) :
        Ed::Layer(page),
        selectBtnLstner(this),
        ellipseBtnLstner(this),
        rectangleBtnLstner(this){

        leftMargins = 13;
        addElement(&controlAnchor);

        //增加工具按钮
        SimpleButton& selectBtn = addButton(1, L"res\\i2_select.png");
        selectBtn.addListener(MouseEvent::PressType, &selectBtnLstner);

        SimpleButton& ellipseBtn = addButton(2,L"res\\ellipse.png");
        ellipseBtn.addListener(MouseEvent::PressType, &ellipseBtnLstner);

        SimpleButton& rectangleBtn = addButton(3,L"res\\rectangle.png");
        rectangleBtn.addListener(MouseEvent::PressType, &rectangleBtnLstner);
      }
     virtual ~ToolsLayer() {
        hash_map<int, SimpleButton*>::iterator it = buttons.begin();
        for(; it != buttons.end(); it++) {
          delete it->second;
        }
      }

     bool isControlAnchor(Element* e){
       return &controlAnchor == e;
     }

     SimpleButton& addButton(int id, const wchar_t* filename) {
       SimpleButton* button = new SimpleButton(filename, leftMargins+buttons.size()*27+3, 3);
        addElement(button);
        buttons[id] = button;
        return *button;
      }

     SimpleButton* getButton(int id){
        return buttons[id];
      }

      protected:
      /**
       * 当鼠标在层上移动时
       */
      void mouseMoveEvent(const MouseEvent* e) {
  //      cout<<"ToolsLayer.mouseMoveEvent, this is "<<this<<endl;
      }
      /**
       * 当鼠标从层上移出时
       */
      void mouseLeaveEvent(const MouseEvent* e) {
  //      cout<<"ToolsLayer.mouseLeaveEvent, this is "<<this<<endl;
      }

      void mousePressEvent(const MouseEvent* e) {
      }


      private:
        real leftMargins;

      public:

      /**
       * 选择模式按钮事件监听器
       */
      class SelectButtonListener : public EventListener{
        public:
          SelectButtonListener(ToolsLayer* toolsLayer)
          :toolsLayer(toolsLayer){}
          virtual ~SelectButtonListener(){}

          virtual bool handle(const Event* e){
            if(toolsLayer->drawPage){
              WorkPage* page = (WorkPage*)toolsLayer->getDrawPage();
              page->setWorkMode(WorkPage::SelectMode);
            }
            return false;
          }
        private:
          ToolsLayer* toolsLayer;
      };

      /**
       * 画椭圆模式按钮事件监听器
       */
      class EllipseButtonListener : public EventListener{
        public:
        EllipseButtonListener(ToolsLayer* toolsLayer)
          :toolsLayer(toolsLayer){}
          virtual ~EllipseButtonListener(){}

          virtual bool handle(const Event* e){
            if(toolsLayer->drawPage){
              WorkPage* page = (WorkPage*)toolsLayer->getDrawPage();
              page->setWorkMode(WorkPage::DrawEllipseMode);
            }
            return false;
          }
        private:
          ToolsLayer* toolsLayer;
      };

      /**
       * 画矩形模式按钮事件监听器
       */
      class RectangleButtonListener : public EventListener{
        public:
        RectangleButtonListener(ToolsLayer* toolsLayer)
          :toolsLayer(toolsLayer){}
          virtual ~RectangleButtonListener(){}

          virtual bool handle(const Event* e){
            if(toolsLayer->drawPage){
              WorkPage* page = (WorkPage*)toolsLayer->getDrawPage();
              page->setWorkMode(WorkPage::DrawRectangleMode);
            }
            return false;
          }
        private:
          ToolsLayer* toolsLayer;
      };

      /**
       * 控制锚点
       */
      class ControlAnchor: public Ed::Element {
        public:
          ControlAnchor():
            movingCursor(LoadCursor(NULL,IDC_SIZEALL))
          {
          brush = new Gdiplus::SolidBrush(Gdiplus::Color(200, 255, 255, 255));

          setCoordinate(3, 2);
          setWidth(5);
          setHeight(26);
          createBoundRect();
        }
        virtual ~ControlAnchor() {
          delete brush;
        }

        /**
         * 画此元素
         */
        void draw(Gdiplus::Graphics* g) {
          g->FillRectangle(brush, *getBoundRect());
          setModified(false);
          setRedraw(false);
        }

//        const Gdiplus::RectF* getBoundRect(){
//          if(!boundRect ) {
//            boundRect = new Gdiplus::RectF(x(), y(), 5.0, layer->height()-4);
//          }
//          return boundRect;
//        }

        protected:
          void mouseMoveEvent(const MouseEvent* e) {
            setCursor(&movingCursor);
            Ed::Element::mouseMoveEvent(e);
          }

          void mousePressEvent(const MouseEvent* e){
            Ed::Element::mousePressEvent(e);
          }

          void mouseLeaveEvent(const MouseEvent* e) {
            resetCursor();
            Ed::Element::mouseLeaveEvent(e);
          }

        private:
          Gdiplus::SolidBrush* brush;
          //十字箭头光标样式
          DefaultCursor movingCursor;
      };

      //控制锚点
      ControlAnchor controlAnchor;
      //所有工具按钮
      hash_map<int, SimpleButton*> buttons;
      //选择模式按钮事件监听器
      SelectButtonListener selectBtnLstner;
      EllipseButtonListener ellipseBtnLstner;
      RectangleButtonListener rectangleBtnLstner;
    };
}
#endif /* TOOLSLAYER_H_ */
